本文介绍HBase Ganos时空引擎功能如何通过API导入数据。

构建SimpleFeature

HBase Ganos通过SimpleFeature类表示空间要素。每个SimpleFeature由ID,Geometry对象与其他属性构成,GeoTools API提供了SimpleFeatureBuilder类帮助用户创建SimpleFeature对象。
SimpleFeatureType sft = ....;
SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(sft);
builder.set("属性名", 属性值);
 ...
builder.set("geom", Geometry);
SimpleFeature feature = builder.buildFeature(object_id + "_" + date.getTime());
            
说明 构建SimpleFeature时,Ganos会默认生成128位的UUID作为Feature ID。为了节省存储空间,用户可以自己指定ID,具体方式可使用以下语句。
SimpleFeature feature =...
feature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE); 

创建Geometry对象

每个SimpleFeature包含一个Geometry对象用来表示要素的空间对象。Geometry的各个空间实体对象定义具体请参见空间对象说明

GeoTools API提供了GeometryFactory工具类帮助用户创建Geometry对象,具体方法如下:
  • 点要素
    • 通过Coordinate对象创建Geometry。
      GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
      Coordinate coord = new Coordinate(1, 1);
      Point point = geometryFactory.createPoint(coord);            
    • 通过WKT语言创建Geometry。Well-known text(简称WKT)是一种文本标记语言,用于表示矢量空间对象、空间参照系统及空间参照系统之间的转换。
       GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
       WKTReader reader = new WKTReader(geometryFactory);
       Point point = (Point) reader.read("POINT (1 1)");
  • 线要素
    • 通过Coordinate对象创建Geometry。
      GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
      Coordinate[] coords  =
       new Coordinate[] {new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6) };
      LineString line = geometryFactory.createLineString(coordinates);
    • 通过WKT语言创建Geometry。
      GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
      WKTReader reader = new WKTReader( geometryFactory );
      LineString line = (LineString) reader.read("LINESTRING(0 2, 2 0, 8 6)");
  • 面要素
    • 通过Coordinate对象创建Geometry。
      GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
      Coordinate[] coords  =
         new Coordinate[] {new Coordinate(4, 0), new Coordinate(2, 2),
                           new Coordinate(4, 4), new Coordinate(6, 2), new Coordinate(4, 0) };
      LinearRing ring = geometryFactory.createLinearRing( coords );
      LinearRing holes[] = null; // use LinearRing[] to represent holes
      Polygon polygon = geometryFactory.createPolygon(ring, holes );        
    • 通过WKT语言创建Geometry。
      GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
      WKTReader reader = new WKTReader( geometryFactory );
      Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");            

数据入库

HBase Ganos通过GeoTools API中的SimpleFeatureWriter写入数据,SimpleFeatureWriter支持事务,可以通过DataStore的getFeatureWriterAppend方法获取。
  • 插入单个SimpleFeature。
    SimpleFeatureType sft = ....;
    SimpleFeatureWriter writer=(SimpleFeatureWriter)ds.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT);
    SimpleFeature toWrite=writer.next();
    toWrite.setAttributes(feature.getAttributes());
    toWrite.getUserData().putAll(feature.getUserData());
    writer.write();
    writer.close();
  • 批量插入SimpleFeature。

    HBase Ganos支持批量插入SimpleFeature,通过GeoTools API中的SimpleFeatureStore类实现。

    List<SimpleFeature> features=...
    SimpleFeatureStore featureStore = (SimpleFeatureStore) ds.getFeatureSource(sft.getTypeName());
    List<FeatureId> featureIds = featureStore.addFeatures(new ListFeatureCollection(sft,features));